package com.taotao.manage.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manage.pojo.ItemCat;
import com.taotao.manage.service.ItemCatService;

@Controller
@RequestMapping("/item/cat")
public class ItemCatController {
	
	@Autowired
	private ItemCatService itemCatService;
	
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<List<ItemCat>> queryItemCatListByParentId(@RequestParam(value="id",defaultValue="0") Long parent_id){
			
			if (parent_id.longValue()<0) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			try {
				ItemCat record = new ItemCat();
				record.setParentId(parent_id);
				List<ItemCat> list = itemCatService.queryListByWhere(record);
				if (list != null) {
					return new ResponseEntity<>(list, HttpStatus.OK);
				} else {
					return new ResponseEntity<>(HttpStatus.NO_CONTENT);
				} 
			} catch (Exception e) {
				e.printStackTrace();
				return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
			
			}
	}
	
	@RequestMapping(value="all",method=RequestMethod.GET)
	public ResponseEntity<Map<String,String>> queryList(){
		List<ItemCat> list = itemCatService.queryAll();
		Map<String, String> hashMap = new HashMap<>();
		for (ItemCat itemCat : list) {
			hashMap.put(itemCat.getId()+"", itemCat.getName());
		}
		return ResponseEntity.ok(hashMap);
				
	}
}
